package net.sx.minemodule.mvp.presenters;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.fastjson.JSON;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.aroutercomment.UserService;
import com.kedll.library.crop.FileUtil;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.Log;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.mvp.views.MineUserInfoView;


import java.io.File;

public class MineUserInfoActivityPresenter extends BasePresenter<MineUserInfoView> {
    private String mImgUrl;

    public MineUserInfoActivityPresenter(MineUserInfoView view) {
        super(view);
    }

    /**
     * 上传文件前信息上报
     *
     * @param sid
     * @param imgUrl
     */
    public void postKDSFile(String sid, String imgUrl) {
        this.mImgUrl = imgUrl;
        File file = new File(imgUrl);
        String md5 = FileUtil.getFileMD5(file);
        long fileSize = FileUtil.getFileSize(file);
        String fileName = FileUtil.getFileName(imgUrl);
        MineApiUtils.postKDSFile(md5, fileName, fileSize, sid, s -> {
            Log.e("===", s);
            postKDSFileSuccess(md5, fileName, file);
        }, throwable -> {

        });
    }

    /**
     * 信息上报成功后调用上传文件
     *
     * @param md5
     * @param fileName
     * @param file
     */
    private void postKDSFileSuccess(String md5, String fileName, File file) {
        MineApiUtils.postFile(md5, fileName, file, s -> {
            String path = JSON.parseObject(s).getString("path");
            modifyUserHeadImg(path);
        }, throwable -> {

        });
    }

    /**
     * 上传文件成功后调用修改用户信息接口修改头像
     *
     * @param path
     */
    private void modifyUserHeadImg(String path) {
        MineApiUtils.modifyUserInfo("SmallPic", path, s -> {

            mView.showPhoto(this.mImgUrl,s);
            ToastUtil.showToast("头像上传成功");
        }, throwable -> {
        });
    }
}
